<template>
  <div id="login">
    <div class="login_box">
      <div class="avatar">
        <img src="../assets/logo.png" alt="">
      </div>
      <el-form ref='loginFormRef' :rules='loginFormRules' :model="loginForm" label-width="0px">
        <!-- 用户名： -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码： -->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <!-- 登录 重置 -->
        <el-form-item class="btns">
          <el-button type="primary" @click='login' >登陆</el-button>
          <el-button  type="info" @click="resetLoginForm" >重置</el-button>
        </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
export default {

  data () {
    return {
      loginForm:{
        username:'admin',
        password:'123456'
        },
      loginFormRules:{
        username:[
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 10, message: '用户名长度在 3 到 10 个字符', trigger: 'blur' }
          ],
    password:[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 5, max: 10, message: '密码长度在 3 到 10 个字符', trigger: 'blur' }
          ]
        }       
      }
    },
  methods:{
    resetLoginForm(){
      // console.log(this.$refs.loginFormRef)
      this.$refs.loginFormRef.resetFields()
    },
    login(){
      this.$refs.loginFormRef.validate( async valid=>{
        if(!valid) return
        var {data:result} = await this.$http.post('login',this.loginForm)
        // console.log(result)
        if(result.meta.status !==200) return this.$message.error('登陆失败')
        this.$message.success('登陆成功')
        window.sessionStorage.setItem('token',result.data.token)
        this.$router.push('/home')
      })
    }
  }



}
</script>

<style lang='less' scoped>
 #login {
   height: 100%;
   width: 100%;
   background: #333744;
 }
 .login_box{
   width: 450px;
   height: 300px;
   background: #fff;
   border-radius: 5px;
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%,-50%)
 }
 .avatar{
   width: 130px;
   height: 130px;
   border-radius: 50%;
   border: 1px solid #333744;
   overflow: hidden;
   padding: 10px;
   position: absolute;
   left: 50%;
   transform: translate(-50%,-50%);
   background: #fff;
   box-shadow: 0px 0px 15px cyan;
   img{
     width: 100%;
     height: 100%;
     background: #ccc;
     border-radius: 50%;
   }
 }
 .btns{
   float: right;
 }
 .el-form{
   position: absolute;
   bottom: 0;
   width: 100%;
   padding: 0 20px;
   box-sizing: border-box;
 }
</style>
